/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.user;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.clientandserver.Constant;
import net.bioteam.appweb.gwt.apps.clientandserver.user.UserAction;
import net.bioteam.appweb.submit.RunStatus;
import net.bioteam.appweb.submit.RunStatusCode;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.History;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeNode;

public class JobDetailGrid extends ListGrid
{

	public RunStatus job;

	public JobDetailGrid()
	{
		setWidth(JobGrid.JobGridWidth-50);
		setHeight100();
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setShowEmptyMessage(true);
		setEmptyMessage("No job is selected");
		setShowRollOver(false);
		setWrapCells(true);
		setCellHeight(40);
		this.setOverflow(Overflow.CLIP_V);
		// setDateFormatter(DateDisplayFormat.TOUSSHORTDATETIME);

		ListGridField nameField = new ListGridField("name", 100);
		ListGridField valueField = new ListGridField("value", JobGrid.JobGridWidth - 155);
		setFields(nameField, valueField);

		addCellClickHandler(new CellClickHandler()
		{
			public void onCellClick(CellClickEvent event)
			{
				if (event.getRowNum()==3)
				{
					History.newItem("user_account_cluster");
				}
			}
		});
	}

	public void updateFileTree()
	{
		updateFileTree(false);
	}
	public void updateFileTree(boolean force)
	{
		if (job==null)
			return;
		JobPanel jPanel=StaticHolder.jobPanel;
		TreeNode jobRootNode=jPanel.getJobRootNode();
		if (jobRootNode.getAttribute("name").equals(String.valueOf(job.getId())))
		{
			if (!force)
				return;
		}
		//jPanel.getJobFiles().show();
		jobRootNode.setAttribute("name", String.valueOf(job.getId()));
		jobRootNode.setAttribute("servername", job.getWorkdirName());
		//hardcode since this node is not loaded from server
		jobRootNode.setAttribute("href", "/" + Constant.APPWEB_CONTEXT + "/apps/user/content/user/job/" + job.getWorkdirName());
		Tree jobFiles=jPanel.getJobFiles().getTree();
		//jPanel.getJobFiles().fetchData();
		jobFiles.reloadChildren(jobRootNode);
		
	}
	public void setJob(RunStatus job)
	{
		this.job = job;
		ListGridRecord rs[] = new ListGridRecord[12];
		ListGridRecord r = null;

		r = new ListGridRecord();
		r.setAttribute("name", "Job ID");
		r.setAttribute("value", job.getId());
		rs[0] = r;

		if (job.isRemoteJob())
		{
			r = new ListGridRecord();
			r.setAttribute("name", "Job ID and DRM ID in remote cluster");
			r.setAttribute("value", job.getRemoteJobId()+"/"+(job.getJobid()==null?"NA":job.getJobid()));
			rs[1] = r;
		} else
		{
			r = new ListGridRecord();
			r.setAttribute("name", "DRM ID (For Local Cluster)");
			r.setAttribute("value", job.getJobid());
			rs[1] = r;
		}

		r = new ListGridRecord();
		r.setAttribute("name", "Application Name");
		r.setAttribute("value", job.getProgramName());
		rs[2] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Cluster ID");
		if (job.getClusterid() == RunStatus.LOCAL_CLUSTER)
		{
			r.setAttribute("value", "Local");
		} else
		{
			r.setAttribute("value", "<span class='fakelink'>"+job.getClusterid()+"</span>");
		}
		rs[3] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Status");
		String status = job.getCode().toString();
		if (status.equals(RunStatusCode.PRESUBMIT.toString()))
		{
			status = "PENDING";
		} else if (status.equals(RunStatusCode.FAILEDSUBMISSION.toString()))
		{
			status = "FAILED";
		} else if (status.equals(RunStatusCode.RETRIEVING.toString()))
		{
			status = "DOWNLOADING";
		}

		r.setAttribute("value", status);
		rs[4] = r;
		
		

		r = new ListGridRecord();
		r.setAttribute("name", "Command Line");
		r.setAttribute("value", job.getCmdline());
		rs[5] = r;

		DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy/MM/dd HH:mm:ss");
		r = new ListGridRecord();
		r.setAttribute("name", "User Submit Time");
		r.setAttribute("value", job.getPreSubmitTime() == null ? "NA" : fmt.format(job.getPreSubmitTime()));
		rs[6] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Submit Time");
		r.setAttribute("value", job.getSubmittedTime() == null ? "NA" : fmt.format(job.getSubmittedTime()));
		rs[7] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Finish Time");
		r.setAttribute("value", job.getFinishedTime() == null ? "NA" : fmt.format(job.getFinishedTime()));
		rs[8] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Communication Attempts #");
		r.setAttribute("value", job.getTrials());
		rs[9] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Note");
		r.setAttribute("value", job.getNote());
		rs[10] = r;

		r = new ListGridRecord();
		r.setAttribute("name", "Job Dir");
		r.setAttribute("value", job.getWorkdirName());
		rs[11] = r;

		
		this.setData(rs);
		updateFileTree(true);
	}

	public void reload(final long jobid)
	{
		StaticHolder.setStatusMessage("reloading job "+jobid, 3);
		ApplicationCallBack<RunStatus> callback = new ApplicationCallBack<RunStatus>()
		{

			@Override
			protected void onFailureAfter(Throwable caught)
			{
				SC.warn(caught.getMessage(), StaticHolder.dumpB);
			}

			@Override
			protected void onSuccessAfter(RunStatus result)
			{
				setJob(result);
			}

			public void execute()
			{
				UserAction.Util.getInstance().refreshJob(jobid, this);
			}
		};
		callback.execute();
	}

	public void reload()
	{
		reload(job.getId());
	}
}
